package jgl.vajra.kingkong.part.home.fragment;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.HomeVehicleTypeBean;
import jgl.vajra.kingkong.databinding.FragmentHomeVehicleTypeBinding;
import jgl.vajra.kingkong.event.HomePageTypeEvent;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.home.contract.HomeVehicleTypeContract;
import jgl.vajra.kingkong.part.home.viewModel.HomeVehicleTypeViewModel;
import jgl.vajra.kingkong.utils.RandomUtils;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMFragment;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;

@CreateViewModel(HomeVehicleTypeViewModel.class)
public class HomeVehicleTypeFragment extends BaseMVVMFragment<HomeVehicleTypeViewModel, FragmentHomeVehicleTypeBinding> implements HomeVehicleTypeContract.View, BaseBindingItemPresenter {

    private String model;
    private SingleTypeBindingAdapter<String> adapter;

    private HomeVehicleTypeBean data;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_vehicle_type;
    }

    @Override
    public void initView() {
        mBinding.setView(this);

        data=new HomeVehicleTypeBean();
        int type = getArguments().getInt("type");
        model = getArguments().getString("model");
        boolean isCalculator = getArguments().getBoolean("isCalculator");


        if (!isCalculator) {
            mBinding.tvPriceCalculator.setVisibility(View.GONE);
        }

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new SingleTypeBindingAdapter<>(mActivity, null, R.layout.item_home_vehicle_type);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
        map.put("model", model);
        mViewModel.getCarInfoData(map);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    //价格计算器
    @Override
    public void onPriceCalculator() {
        IntentManager.toPriceCalculatorActivity(mActivity);
    }

    //车型详情
    @Override
    public void onVehicleDetails() {
        IntentManager.toVehicleDetailsActivity(mActivity, data);
    }

    @Override
    public void returnHomeCarInfoBean(HomeVehicleTypeBean data) {
        this.data=data;
        mBinding.setData(data);

        List<String> list=new ArrayList<>();
        if (data.carParams!=null&&data.carParams.size() >= data.displayNumber) {
            for (int i = 0; i < data.carParams.size(); i++) {
                if(i<data.displayNumber){
                    list.add(data.carParams.get(i));
                }
            }
        }
        adapter.refresh(list);
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHomePageTypeEvent(HomePageTypeEvent event) {
    }

    @Override
    public void onItemClick(int position, Object itemData) {

    }
}
